/****************************************************************

this file is part of Music Practice Audio Player
Copyright (C)2012 Serhii Ratsyborynskyi (ratz@live.ru)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

******************************************************************/

#include "CPMpaMultitrackController.hpp"
#include <QDebug>


CPMpaMultitrackController::CPMpaMultitrackController(QStringList channelList,
                                                     QWidget *parent) :QWidget( parent)
{
    initGrafics( channelList );
}

void CPMpaMultitrackController::channelVolumeChanged(int channel, int volume)
{
    mVolumeList[channel]=volume;
    emit signalSetChannelVolume(mVolumeList);
}

void CPMpaMultitrackController::channelMute(int channel, bool mute)
{
    qDebug() << "Channel mute" << channel << " mute " << mute;
    mMuteList[channel]=mute;
    updateSoloMuteBus();
}

void CPMpaMultitrackController::channelSolo(int channel, bool solo)
{
    mSoloList[channel]=solo;
    updateSoloMuteBus();
}

void CPMpaMultitrackController::initGrafics(const QStringList &channelList)
{
    QVBoxLayout * pVBoxLayout = new QVBoxLayout(this);

    for(int i = 0; i < channelList.size(); i++ )
    {
        TrackController * pTrackController = new TrackController(i, channelList[i], this);
        connect( pTrackController, SIGNAL(signalMute(int,bool)), this, SLOT(channelMute(int,bool) ));
        connect( pTrackController, SIGNAL(signalSolo(int,bool)), this, SLOT(channelSolo(int,bool) ));
        connect( pTrackController, SIGNAL(signalVolume(int,int)), this, SLOT(channelVolumeChanged(int,int) ));
        pVBoxLayout->addWidget(pTrackController);
        mMuteList.push_back(false);
        mSoloList.push_back(false);
        mVolumeList.push_back(80);
    }
    setLayout(pVBoxLayout);
}

void CPMpaMultitrackController::updateSoloMuteBus()
{
    QList<bool> muteMatrix;
    bool sendSignal=false;
    for( int i = 0; i < mSoloList.size(); ++i )
    {
        if(mSoloList[i]==true)
        {
            for(int i = 0; i<mSoloList.size(); ++i)
            {
                muteMatrix.push_back(!mSoloList[i]);
            }
            sendSignal = true;
            break;
        }
    }
    if( sendSignal != true )
    {
        for( int i = 0; i < mSoloList.size(); i++)
        {
            if( mMuteList[i]== true)
            {
                muteMatrix = mMuteList;
            }
            sendSignal = true;
            break;
        }
    }
    if(sendSignal)
    {
        qDebug() << "Mute bus";
        emit signalMute(muteMatrix);
    }
}
